package calabashandroidplugin.popup.actions;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.part.FileEditorInput;

import calabashandroidplugin.utils.Executor;
import calabashandroidplugin.utils.PathExtractor;

public class RunTest implements IObjectActionDelegate {
	private Shell shell;

	public RunTest() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {

		PathExtractor extractor = new PathExtractor();
		
		// New Executor object
		Executor exec = new Executor();
		Scanner s = exec.execute("run", "bin/" + extractor.getProjectName() + ".apk",
				extractor.getFullPath());

		// Creating a new console view
		MessageConsole myConsole = findConsole("Calabash Android");
		MessageConsoleStream out = myConsole.newMessageStream();
		out.println("->Running calabash-test");
		out.println(s.next());
		
		// Close Scanner manually
		s.close();
		
	
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	public Scanner executor(String command, String pathToApk, String projectPath) {
		ProcessBuilder builder = new ProcessBuilder("cmd", "/c",
				"calabash-android", command, pathToApk);
		builder.directory(new File(projectPath));
		Process p;

		Scanner s = null;
		try {
			p = builder.start();
			s = new Scanner(p.getInputStream()).useDelimiter("\\Z");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return s;
	}	
	

	public String getProjectName() {
		IEditorPart editorPart = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		String activeProjectName = null;

		if (editorPart != null) {
			IFileEditorInput input = (FileEditorInput) editorPart
					.getEditorInput();
			IFile file = input.getFile();
			IProject activeProject = file.getProject();
			activeProjectName = activeProject.getName();
		}
		return activeProjectName;
	}

	public String getWorkspacePath() {
		IPath configPath = ResourcesPlugin.getWorkspace().getRoot()
				.getLocation();
		String workspacePath = configPath.toString();
		return workspacePath;
	}

	public String getFullPath() {
		return getWorkspacePath() + "/" + getProjectName();

	}

	private MessageConsole findConsole(String name) {
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager conMan = plugin.getConsoleManager();
		IConsole[] existing = conMan.getConsoles();
		for (int i = 0; i < existing.length; i++)
			if (name.equals(existing[i].getName()))
				return (MessageConsole) existing[i];
		// no console found, so create a new one
		MessageConsole myConsole = new MessageConsole(name, null);
		conMan.addConsoles(new IConsole[] { myConsole });
		return myConsole;
	}
}
